﻿<?xml version="1.0" encoding="utf-8"?><Articles><Article><ArticlesID>554</ArticlesID><Title>Lấy ng&amp;#224;y đầu ti&amp;#234;n trong tuần d&amp;#249;ng SQL Server</Title><Body>&amp;lt;p&amp;gt;Tạo 1 &amp;lt;strong&amp;gt;Function&amp;lt;/strong&amp;gt; như sau:&amp;lt;/p&amp;gt;
&amp;lt;pre class=&amp;quot;brush: sql;&amp;quot; title=&amp;quot;code&amp;quot;&amp;gt;
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


CREATE FUNCTION dbo.F_START_OF_WEEK
(
	@DATE			datetime,
	-- Sun = 1, Mon = 2, Tue = 3, Wed = 4
	-- Thu = 5, Fri = 6, Sat = 7
	-- Default to Sunday
	@WEEK_START_DAY		int	= 1	
)
/*
Find the fisrt date on or before @DATE that matches 
day of week of @WEEK_START_DAY.
*/
returns		datetime
as
begin
declare	 @START_OF_WEEK_DATE	datetime
declare	 @FIRST_BOW		datetime

-- Check for valid day of week
if @WEEK_START_DAY between 1 and 7
	begin
	-- Find first day on or after 1753/1/1 (-53690)
	-- matching day of week of @WEEK_START_DAY
	-- 1753/1/1 is earliest possible SQL Server date.
	select @FIRST_BOW = convert(datetime,-53690+((@WEEK_START_DAY+5)%7))
	-- Verify beginning of week not before 1753/1/1
	if @DATE &amp;amp;gt;= @FIRST_BOW
		begin
		select @START_OF_WEEK_DATE = 
		dateadd(dd,(datediff(dd,@FIRST_BOW,@DATE)/7)*7,@FIRST_BOW)
		end
	end

return @START_OF_WEEK_DATE

end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Test thử&amp;lt;/p&amp;gt;
&amp;lt;pre class=&amp;quot;brush: sql;&amp;quot; title=&amp;quot;code&amp;quot;&amp;gt;
SELECT dbo.F_START_OF_WEEK(GETDATE(),1)&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;N&amp;amp;oacute; sẽ trả về ng&amp;amp;agrave;y đầu ti&amp;amp;ecirc;n trong tuần:&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;V&amp;amp;iacute; dụ: H&amp;amp;ocirc;m nay ng&amp;amp;agrave;y 27/10/2009 sau khi &amp;lt;strong&amp;gt;SELECT&amp;lt;/strong&amp;gt; ng&amp;amp;agrave;y đầu ti&amp;amp;ecirc;n trong tuần l&amp;amp;agrave; 25/10/2009 t&amp;amp;iacute;nh từ ng&amp;amp;agrave;y Chủ nhật&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Bạn c&amp;amp;oacute; thể Customize ng&amp;amp;agrave;y đầu ti&amp;amp;ecirc;n l&amp;amp;agrave; thứ mấy tại tham số thứ 2 &amp;lt;strong&amp;gt;@Week_Start_Day&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;1. Chủ Nhật&amp;lt;br /&amp;gt;
2. Thứ 2&amp;lt;br /&amp;gt;
3. Thứ 3&amp;lt;br /&amp;gt;
4. Thứ 4&amp;lt;br /&amp;gt;
5. Thứ 5&amp;lt;br /&amp;gt;
6. Thứ 6&amp;lt;br /&amp;gt;
7. Thứ 7&amp;lt;br /&amp;gt;
&amp;amp;nbsp;&amp;lt;/p&amp;gt;</Body><CommentEnabled>True</CommentEnabled><ViewCount>0</ViewCount><ReleaseDate>10/27/2009 2:01:33 AM</ReleaseDate><TotalComment>0</TotalComment><IsMedia>False</IsMedia><IsPicture>False</IsPicture><IsVote>True</IsVote><ArticlePassword /><PostVisible>0</PostVisible><ArticlePath>/2009/10/27/Lay-ngay-dau-tien-trong-tuan-dung-SQL-Server-22A</ArticlePath><Approved>True</Approved><ImagePath /><CategoryId>35</CategoryId><ArrayCatID>35|25</ArrayCatID></Article></Articles>